// 全局参数
var param = {
	userID : 0,
	userCode : "",
	userName : ""

}
var deleteIndex;//全局变量
//初始化变量
function initParam() {
	
}

//初始化layer内容
function initLayer(){
	
	layui.use('element', function(){
        var $ = layui.jquery, element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
        
        element.on('tab(layerTab)', function(data){
            deleteIndex=$(this).attr("lay-id");
        });
        initLeftMenu(element);
        
    });
}

//左侧导航栏初始化
function initLeftMenu(element){
	$.ajax({
		type: 'post',
		url: '/base/bardmenu/getMenuList',
		dataType: 'json',
		success:function(result){
			var code = result.code;
			if(code==0){
				var menuList = result.data;
				initMenuData(menuList);
				initLayerTab(element);
				element.render('nav');
			}
		}
	});
}

//初始化tab组件
function initLayerTab(element){
	//触发事件
    var active = {
      tabAdd: function(othis){////增加指定Tab项
    	  var $othis = $(othis[0]);
    	  var code = $othis.attr("code");
    	  var exist = $("li[lay-id='"+code+"']").length;
    	  var h =  window.innerHeight || document.documentElement.clientHeight;
    	  h = h*0.76;
    	  if(exist<1){
        	  var title = $othis.text();
        	  var url = $othis.attr("url");
        	  var content = '<div class="layui-tab-item layui-show" style="overflow:hidden;">';
        	  content += '<iframe src="' + url + '" frameborder="0" class="layadmin-iframe"  height="'+h+'"></iframe>';
        	  content += '</div>';
        	  //新增一个Tab项
        	  element.tabAdd('layerTab', {
        		  title: title,
        		  content: content,
        		  id: code
        	  });
        	  CustomRightClick(code);//绑定右键菜单
    	  }
		  element.tabChange('layerTab', code); //切换到：用户管理
      }
      ,tabDelete: function(othis){//删除指定Tab项
    	  element.tabDelete("layerTab", othis);//删除
      },
      tabDeleteAll: function (ids) {//删除所有
          $.each(ids, function (i,item) {
              element.tabDelete("layerTab", item);
          })
      }
    };
    
    $('.site-demo-active').on('click', function(){
    	var othis = $(this), type = othis.data('type');
    	active[type] ? active[type].call(this, othis) : '';
    });
    
    //就需要给右键添加功能
    $(".rightmenu li").click(function () {
        if ($(this).attr("data-type") == "closeOther") {//关闭其他
            $.each($(".layui-tab-title li[lay-id]"), function () {
                //如果点击左侧菜单栏所传入的id 在右侧tab项中的lay-id属性可以找到，则说明该tab项已经打开
                if ($(this).attr("lay-id")!=deleteIndex) {
                    active.tabDelete($(this).attr("lay-id"));
                }
            });
        } else if ($(this).attr("data-type") == "closeall") {//关闭所有
            var tabtitle = $(".layui-tab-title li");
            var ids = new Array();
            $.each(tabtitle, function (i) {
                ids[i] = $(this).attr("lay-id");
            });
            active.tabDeleteAll(ids);
        }
        $('.rightmenu').hide();
    });
}

//装配菜单模板
function initMenuData(data){
	var ul = $("<ul class='layui-nav layui-nav-tree' lay-filter='test'></ul>");
    for(var i=0;i < data.length;i++){
        var node = data[i];
        var li = $("<li class='layui-nav-item'></li>");
        var a = $("<a class='' href='javascript:;'>"+node.name+"</a>");
        li.append(a);
        //获取子节点
        var childs = node.childs;
        if(childs.length>0){
            a.append("<span class='layui-nav-more'></span>");
            var dl = $("<dl class='layui-nav-child'></dl>");
            for (var y in childs) {
                var dd = $("<dd><a class='site-demo-active' style='cursor: pointer;' data-type='tabAdd' url='" + childs[y].url + "' code='"+childs[y].code+"'>"+childs[y].name+"</a></dd>");
                //判断选中状态
                dl.append(dd);
            }
            li.append(dl);
        }
        ul.append(li);
    }
    $(".layui-side-scroll").append(ul);
}

//绑定右键菜单
function CustomRightClick(id) {
    //取消右键
    $('.layui-tab-title li').on('contextmenu', function () { return false; });
    $('.layui-tab-title,.layui-tab-title li').click(function () {
        $('.rightmenu').hide();
    });
    //桌面点击右击
    $('.layui-tab-title li').on('contextmenu', function (e) {
        var popupmenu = $(".rightmenu");
        popupmenu.find("li").attr("data-id",id);
        l=  e.clientX-220;
        t=	e.clientY-40;
        popupmenu.css({ left: l, top: t }).show();
        return false;
    });
}

//退出登录
function logOut(){
	$.ajax({
		type: 'get',
		url: '/base/login/logOut',
		dataType: 'json',
		success:function(result){
			var rtn = result.rtn;
			if(rtn=="ok"){
				sessionStorage.removeItem("name", "");
				window.location.href = "/";
			}
		}
	});
}

//初始化
function initHome(){
	
	initParam();//初始化变量
	
	initLayer();//初始化layerUI
	
}

window.onload = function() {
	initHome();//初始化
	var name = sessionStorage.getItem("name");
	$("#name").text(name);
}


